package com.hdu.hot100.oneDimensionalDp;


public class lc32 {
    class Solution {
        public int longestValidParentheses(String s) {
            int len = s.length();
            int[] dp = new int[len];
            int ans = 0;
            for (int i = 1; i < len; i++)
            {
                if (s.charAt(i) == ')')
                {
                    int p = i - dp[i - 1] - 1;
                    if (p >= 0 && s.charAt(p) == '(')
                    {
                        dp[i] = dp[i - 1] + 2 + (p > 0 ? dp[p - 1] : 0);
                    }
                    ans = Math.max(ans, dp[i]);
                }
            }
            return ans;
        }
    }
}
